<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        v-permission="['main_station.add_news']"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="$refs.edit.handleCreate()"
        >添加</el-button
      >
    </div>

    <el-table
      v-loading.body="listLoading"
      :data="list"
      element-loading-text="加载中 ..."
      border
      highlight-current-row
    >
      <el-table-column type="index" align="center" label="序号" width="50" />
      <el-table-column label="ID" width="120" align="center" prop="number" />
      <el-table-column label="标题" width="500" prop="title" show-overflow-tooltip />
      <el-table-column label="添加日期" width="150" align="center" prop="publish_date" />
      <el-table-column label="操作" min-width="150px">
        <template slot-scope="scope">
          <el-button
            v-permission="['main_station.change_news']"
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="$refs.edit.handleEdit(scope.row.uuid)"
          />
          <el-button
            v-permission="['main_station.delete_news']"
            type="primary"
            size="mini"
            icon="el-icon-delete"
            @click="handleDel(scope.row.uuid)"
          />
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <pagination
        :total="count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.size"
        @pagination="fetchData"
      />
    </div>

    <edit ref="edit" />
  </div>
</template>

<script>
import { getList, del } from '@/api/examples'
import Edit from './components/Edit'

export default {
  name: 'NewsIndex',
  components: {
    Edit
  },
  data() {
    return {
      list: null,
      count: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取数据
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then((res) => {
        this.list = res.data.results
        this.count = res.data.count
        this.listLoading = false
      })
    },
    // 删除
    handleDel(uuid) {
      this.$confirm('确认删除该项目吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(uuid).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success'
            })
            this.fetchData()
          })
        })
        .catch(() => {})
    }
  }
}
</script>
